<?php
namespace Common\Common;

abstract class Model extends \Think\Model{

    protected $tablePrimary;

    /**
     * 生成对象
     * @param array $data
     * @return mixed
     */
    public function make(array $data=[]){
        $className = substr(static::class, 0, -5);
        $className = $className.'Object';
        return new $className($data, $this);
    }

    /**
     * 通过主键获取对象
     * @return Model
     */
    public function fetch(){
        if(func_num_args() > 0) {
            $args = func_get_args();
            if (is_string($this->tablePrimary))
                $this->tablePrimary = [$this->tablePrimary];
            $condition = [];
            foreach ($this->tablePrimary as $key => $value)
                $condition[$value] = $args[$key];
            $this->where($condition);
        }
        $data = $this->find();
        return $this->make($data);
    }

    /**
     * 通过主键获取对象
     * @return array
     */
    public function fetchList(){
        $dataList = $this->select();
        $result = [];
        foreach ($dataList as $value)
            $result[] = $this->make($value);
        return $result;
    }

    public function save($data='',$options=array()) {
        if($data instanceof Model){
            
        }
        parent::save($data, $options);
    }

    public function delete(){

    }

}